using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace WindowsGame1.CoreComponents
{
    public class GameScreen : Microsoft.Xna.Framework.DrawableGameComponent
    {
        //holds the components of the screen in a list
        protected List<GameComponent> childComponents;


        protected SpriteBatch spriteBatch;
        protected ContentManager Content;
        protected Game1 game;

        /////////////////////
        /////Constructor/////
        /////////////////////
        /// <summary>
        /// Initializes the list of components
        /// and sets the visible and enabled variables to false
        /// so that the screen does not update unless
        /// you specifically tell it to
        /// </summary>
        /// <param name="game"></param>
        public GameScreen(Game1 game)
            : base(game)
        {
            this.game = game;
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            Content = (ContentManager)Game.Services.GetService(typeof(ContentManager));

            childComponents = new List<GameComponent>();
            Visible = false;
            Enabled = false;
        }

        //Returns the components of the screen
        public List<GameComponent> Components
        {
            get { return childComponents; }
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// This goes through the components in the list and 
        /// if the component is in enabled, then it is updated
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            foreach (GameComponent child in childComponents)
            {
                if (child.Enabled)
                {
                    child.Update(gameTime);
                }
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// Similar to the Update method, this method goes through
        /// the components and calls their draw methods if the
        /// component is both drawable and visible.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            foreach (GameComponent child in childComponents)
            {
                if ((child is DrawableGameComponent) &&
                ((DrawableGameComponent)child).Visible)
                {
                    ((DrawableGameComponent)child).Draw(gameTime);
                }
            }
            base.Draw(gameTime);
        }

        /// <summary>
        /// Shows a component by making it visible and enabled
        /// </summary>
        public virtual void Show()
        {
            Visible = true;
            Enabled = true;
        }

        /// <summary>
        /// Hides a component by setting it to invisible and disabled
        /// </summary>
        public virtual void Hide()
        {
            Visible = false;
            Enabled = false;
        }
    }
}